import VueRouter from "vue-router";
import Vue from 'vue';
import AddGoods from "@/pages/AddGoods.vue";
import GoodsList from "@/pages/GoodsList.vue";
import MyCar from "@/pages/MyCar.vue";
import Login from "@/pages/Login.vue";
Vue.use(VueRouter);
const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            component:AddGoods
        },
        {
            path:'/goodslist',
            component:GoodsList
        },
        {
            path:'/mycar',
            component:MyCar,
            meta:{
                isAuthorization:true
            }
        },{
            path:'/login',
            component:Login,
            meta:{
                hideNav:true
            }
        }
    ]
});
router.beforeEach((to, from, next) => {
    if(to.meta.isAuthorization){
        if(localStorage.getItem('token')){
            next()
        }else{
            next({
               path: '/login',
                returnUrl:to.fullPath
            });
        }
    }else{
        next()
    }
})
export default router;